<template>
  <div class="o-input-number">
    <w-input
      v-model="value"
      @on-change="onChange"
      @on-blur="onBlur"
      @on-focus="onFocus"
      @on-click="onClick"
      @on-dochange="onDochange"
      @on-enter="onEnter"
      @on-mouseleave="onMouseleave"
      @on-mouseenter="onMouseenter"
      :disabled="renderOpts.props.disabled"
      :placeholder="renderOpts.props.placeholder"
      :align="align"
      ref="input"
    ></w-input>
  </div>
</template>
<script lang="ts">
import { Tsrv, vue, T_srv } from '@/services';
import wInput from './input.vue';

@T_srv.comp({
  components: {
    wInput
  }
})
export default class oNumberInput extends vue {
  // modify by wiz
  public compName: string = 'oNumberInput';

  /**
   * props
   */
  @T_srv.prop() renderOpts: any;

  @T_srv.prop() params: any;

  private value = null;

  private startValue = null;

  private align = 'left';

  /**
   * data
   */
  @T_srv.watch('params')
  onParamsChange(e) {
    this.initProps();
    this.setValue();
  }

  mounted() {
    this.initProps();
    this.setValue();
    // this.addCustomEvents();
  }

  private initProps() {
    if (this.params.property) {
      const items = this.params.$form.items;
      const find = Tsrv.utils.find(
        items,
        el => el.field === this.params.property
      );
      if (find) {
        this.align = find.align || 'left';
      }
    } else {
      this.align = this.params.column.align || 'left';
    }
  }

  public focus() {
    (this.$refs.input as wInput).focus();
  }

  onClick() {
    this.startValue = this.getValue();
  }

  onDochange() {
    if (this.startValue !== this.getValue()) {
      this.emitEvent('dochange');
    }
  }

  resetValue() {
    this.setValue(null);
    this.setTableValue(null);
  }

  setValue(val: null | string | undefined = undefined) {
    if (this.params.property) {
      this.value =
        val !== undefined ? val : this.params.data[this.params.property];
    } else {
      this.value =
        val !== undefined ? val : this.params.row[this.params.column.property];
    }
  }

  setTableValue(val: string | number | null) {
    const numberVal: string | number | null = val;
    // const numberVal: number | null =
    //   val === null ? null : Tsrv.utils.toNumber(val);
    if (this.params.property) {
      this.params.data[this.params.property] = numberVal;
    } else {
      this.params.row[this.params.column.property] = numberVal;
    }
  }

  onBlur() {
    this.setTableValue(Tsrv.utils.toNumber(this.value));
    this.emitEvent('blur');
    if (this.startValue !== this.getValue()) {
      this.emitEvent('dochange');
    }
  }

  onFocus() {
    this.startValue = this.getValue();
    this.emitEvent('focus');
  }

  onChange(e) {
    this.setTableValue(e);
    this.emitEvent('change');
  }

  onEnter(e) {
    this.emitEvent('enter');
  }

  onMouseleave(e) {
    this.emitEvent('mouseleave');
  }

  onMouseenter(e) {
    this.emitEvent('mouseenter');
  }

  getValue() {
    return this.params.property
      ? this.params.data[this.params.property]
      : this.params.row[this.params.column.property];
  }

  emitEvent(event: string) {
    const ev = this.renderOpts.events;
    // 深拷贝有问题
    // const params = Tsrv.utils.clone(this.params);
    const params = Tsrv.utils.cloneNoMatchVue(this.params, true);
    params.from = 'inputnumber';
    for (const action in ev) {
      if (ev[action] && action === `on-${event}`) {
        // ev[action](params);
        ev[action](params, event);
      }
    }
  }

  // beforeDestroy() {
  //   this.destroyCustomEvents();
  // }
}
</script>
